--TD 4.1.2 Unpowered CC Voltage Test
baselib.report(0x00, "TD4.1.2 beginning.")                 --report:参数1是上报的消息的MsgType字段,参数2是content内容
--第一步:如果Type_C_State_Machine是DRP,CVS验证Type_C_Power_Source未设置为1(UFP-Powered)
--原文:If Type_C_State_Machine is DRP, CVS verifies that Type_C_Power_Source is not set to 1 (UFP-Powered) [TD.4.1.2.V.1]
if Type_C_State_Machine == 2 then
    if Type_C_Power_Source == 1 then                       --为1说明是UFP-Powered
        baselib.report(0x00, "TD.4.1.2.V.1 fail.")
    else
        baselib.report(0x00, "TD.4.1.2.V.1 pass.")
    end
end
--第二步:CVS提示用户断开设备的外部电源,使PUT处于断电状态      --原文:CVS prompts the user to unplug external power to the product so that the PUT is unpowered
baselib.report(0x00, "Please disconnect the external power supply of the device.")           --疑问:除此之外CVS还需要什么操作吗???
--第三步:CVS在USB默认电流范围内应用Rp                        --原文:CVS applies Rp in the USB Default Current range
busilib.SetSwitchState(SW.CC1_RP_0A5, 1)
--第四步:如果Type_C_State_Machine是DRP且Type_C_Power_Source是2(Both,双电源),CVS验证PUT应用Rd且不切换状态
--原文:If Type_C_State_Machine is DRP and Type_C_Power_Source is 2 (Both), CVS verifies that PUT applies Rd and does not toggle. [TD.4.1.2.V.2]
if Type_C_State_Machine == 2 and Type_C_Power_Source == 2 then
    local monitor1 = busilib.newMonitor(TMD.CHECK_POINT, 0, {2}, {0}, {0})                   --监测CC1是否有超过1600mV
    baselib.delay(100)                                     --这边我想法是因为要验证不切换状态,等待100ms如果CC上电压没有超出范围就认为状态不变
    if busilib.getMonitorResult(monitor1) == 1 then        --出现1代表超过1600mv,检查点fail
        baselib.report(0x00, "TD.4.1.2.V.2 fail.")
    else
        baselib.report(0x00, "TD.4.1.2.V.2 pass.")
    end
    busilib.deleteMonitor(monitor1)
else
--第五步:否则,CVS验证CC引脚电压在vOPEN范围内                 --原文:Else, CVS verifies that CC voltage is in vOPEN range [TD.4.1.2.V.3]
    local VCC1 = busilib.GetADC(ADC.CC1, 4)                --获取CC1电压,vOPEN范围应该是大于1.65V
    if VCC1 > 1650 then
        baselib.report(0x00, "TD.4.1.2.V.3 pass.")
    else
        baselib.report(0x00, "TD.4.1.2.V.3 fail.")
    end
end
--第六步:CVS进入Disabled状态并保持500ms                     --原文:CVS transitions to Disabled for 500ms
busilib.setUsbCcs(CCS.Disabled)      
baselib.delay(500) 
--第七步:CVS在USB Type-C 1.5A电流范围内应用Rp               --原文:CVS applies Rp in the USB Type-C Current 1.5 A range
busilib.SetSwitchState(SW.CC1_RP_0A5, 0)                   --先关掉之前的0.5A
busilib.SetSwitchState(SW.CC1_RP_1A5, 1)                   --在1.5A电流范围应用Rp
--第八步:如果Type_C_State_Machine是 DRP且Type_C_Power_Source是2(Both),CVS 验证PUT应用Rd且不切换状态
--原文:If Type_C_State_Machine is DRP and Type_C_Power_Source is 2 (Both), CVS verifies that PUT applies Rd and does not toggle. [TD.4.1.2.V.4]
if Type_C_State_Machine == 2 and Type_C_Power_Source == 2 then                               --下面几步和第四步一样
    local monitor1 = busilib.newMonitor(TMD.CHECK_POINT, 0, {2}, {0}, {0})                   --监测CC1是否有超过1600mV
    baselib.delay(100)                              
    if busilib.getMonitorResult(monitor1) == 1 then        --出现1代表超过1600mv,检查点fail
        baselib.report(0x00, "TD.4.1.2.V.4 fail.")
    else
        baselib.report(0x00, "TD.4.1.2.V.4 pass.")
    end
    busilib.deleteMonitor(monitor1)
else
--第九步:否则,CVS验证CC引脚电压在vOPEN范围内                 --原文:Else, CVS verifies that CC voltage is in vOPEN range [TD.4.1.2.V.5]
    local VCC1 = busilib.GetADC(ADC.CC1, 4)                --获取CC1电压,vOPEN范围应该是大于1.65V
    if VCC1 > 1650 then
        baselib.report(0x00, "TD.4.1.2.V.5 pass.")
    else
        baselib.report(0x00, "TD.4.1.2.V.5 fail.")
    end
end
--第十步:CVS进入Disabled状态并保持500ms                     --原文:CVS transitions to Disabled for 500ms
busilib.setUsbCcs(CCS.Disabled)      
baselib.delay(500) 
--第十一步:CVS在USB Type-C 3.0A电流范围内应用Rp             --原文:CVS applies Rp in the USB Type-C Current 3.0 A range
busilib.SetSwitchState(SW.CC1_RP_1A5, 0)                   --先关掉之前的1.5A
busilib.SetSwitchState(SW.CC1_RP_3A, 1)                    --在3.0A电流范围应用Rp
--第十二步:如果Type_C_State_Machine是 DRP且Type_C_Power_Source是2(Both),CVS 验证PUT应用Rd且不切换状态
--原文:If Type_C_State_Machine is DRP and Type_C_Power_Source is 2 (Both), CVS verifies that PUT applies Rd and does not toggle. [TD.4.1.2.V.6]
if Type_C_State_Machine == 2 and Type_C_Power_Source == 2 then                               --下面几步和第四步一样
    local monitor1 = busilib.newMonitor(TMD.CHECK_POINT, 0, {2}, {0}, {0})                   --监测CC1是否有超过1600mV
    baselib.delay(100)                              
    if busilib.getMonitorResult(monitor1) == 1 then        --出现1代表超过1600mv,检查点fail
        baselib.report(0x00, "TD.4.1.2.V.6 fail.")
    else
        baselib.report(0x00, "TD.4.1.2.V.6 pass.")
    end
    busilib.deleteMonitor(monitor1)
else
--第十三步:否则,CVS验证CC引脚电压在vOPEN范围内               --原文:Else, CVS verifies that CC voltage is in vOPEN range [TD.4.1.2.V.7]
    local VCC1 = busilib.GetADC(ADC.CC1, 4)                --获取CC1电压,vOPEN范围应该是大于1.65V
    if VCC1 > 1650 then
        baselib.report(0x00, "TD.4.1.2.V.7 pass.")
    else
        baselib.report(0x00, "TD.4.1.2.V.7 fail.")
    end
end
--第十四步:CVS提示测试操作员重新连接设备的外部电源,使PUT恢复供电状态       --原文:CVS prompts the test operator to replug external power to the product so that the PUT is powered
baselib.report(0x00, "Please restore the device to power.")            --疑问:除此之外CVS还需要什么操作吗???
baselib.report(0x00, "Demo is done here.")
baselib.report(0x07, "\x04")